package com.my.test.thread.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class TestReentrantLock {

    private static Lock lock = new ReentrantLock(); // 可重入锁/排它锁

    public static void main(String[] args) throws InterruptedException {
        try {
            lock.lock(); //获得锁
            lock.lockInterruptibly(); //可中断(中断当前线程)的获得锁
            lock.tryLock(); // 尝试非阻塞获得锁
            lock.tryLock(1000, TimeUnit.MILLISECONDS); // 超时获得锁
        } finally {
            lock.unlock(); // 释放锁
        }
    }

}
